<?php

namespace App\Controller\Backend\User;

use App\Controller\AbstractController;
use App\Service\System\CaptchaService;
use App\Service\User\UserService;
use Hyperf\Di\Annotation\Inject;

class UserController extends AbstractController
{

    #[Inject]
    public UserService $userService;

    public function getUserPuKey(){
        $account = $this->request->post('account','');
        $validator = $this->validationFactory->make(
            [
                'account' => $account
            ],
            [
                'account' => 'required'
            ],
            [
                'account.required' => '用户账号不能为空'
            ]
        );
        if ($validator->fails()){
            apiError($validator->errors()->first());
        }
        return success(
            [
                'pu_key' => $this->userService->getUserPuKey($account)
            ]
        );
    }

    public function accountLogin(){
        $data = [
            'user_account' => $this->request->post('account',''),
            'password' => $this->request->post('password',''),
            'key' => $this->request->post('key',''),
            'captcha' => $this->request->post('captcha',''),
            'captcha_key' => $this->request->post('captcha_key',''),
        ];
        $validator = $this->validationFactory->make(
            $data,
            [
                'user_account' => 'required',
                'password' => 'required',
                'key' => 'required',
                'captcha' => 'required',
                'captcha_key' => 'required',
            ],
            [
                'user_account.required' => '账号不能为空',
                'password.required' => '密码不能为空',
                'key.required' => 'key不能为空',
                'captcha.required' => '验证码不能为空',
                'captcha_key.required' => '验证码key不能为空',
            ]
        );
        if ($validator->fails()){
            apiError($validator->errors()->first());
        }

        // 校验验证码
        (new CaptchaService())->checkCaptcha($data['captcha_key'],$data['captcha']);

        return success($this->userService->getUserInfoByAccountPassowrd($data));


    }

    public function login(){
        $data = [
            'phone' => $this->request->post('account',''),
            'captcha' => $this->request->post('captcha','')
        ];
        $validator = $this->validationFactory->make(
            $data,
            [
                'phone' => 'required',
                'captcha' => 'required',
            ],
            [
                'phone.required' => '账号不能为空',
                'captcha.required' => '验证码不能为空',
            ]
        );
        return error(null,"接口关闭");
    }

    public function addUser(){
        $data = [
            'division_id' => $this->request->post('division_id',0),
            'user_name' => $this->request->post('user_name',''),
            'user_account' => $this->request->post('user_account',''),
            'user_password' => $this->request->post('user_password',''),
            'user_phone' => $this->request->post('user_phone',''),
            'user_email' => $this->request->post('user_email',''),
            'user_type' => $this->request->post('user_type',''),
        ];
        $validator = $this->validationFactory->make(
            $data,
            [
                'division_id' => 'required',
                'user_name' => 'required',
                'user_account' => 'required',
                'user_password' => 'required',
                'user_phone' => 'required',
                'user_type' => 'required|in:2,3',
            ],
            [
                'division_id.required' => '机构不能为空',
                'user_name.required' => '用户名不能为空',
                'user_account.required' => '账号不能为空',
                'user_password.required' => '用户密码不能为空',
                'user_phone.required' => '手机号不能为空',
                'user_type.required' => '用户类型不能为空',
                'user_type.in' => '用户类型错误',
            ]
        );
        if ($validator->fails()){
            apiError($validator->errors()->first());
        }

        $res = $this->userService->addUser($data);
        return success($res);

    }

    public function getUserListByDivision(int $divisionId){
        $filters = [];
        $res = $this->userService->getUserListByDivision($divisionId,$filters);
        return success($res);
    }




}